package com.czb.servlet;

import com.czb.bean.Rend;
import com.czb.service.RecordServcieImpl;
import com.czb.service.RecordService;
import com.czb.service.RendService;
import com.czb.service.RendServiceImpl;
import com.czb.util.PageBean;
import com.czb.util.ResultJson;
import com.czb.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/RendServlet")
public class RendServlet extends HttpServlet {
    RendService rendService = new RendServiceImpl();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        if(action.equals("findAll")){
            findAll(req,resp);
        }else if(action.equals("add")){
            add(req,resp);
        }else if(action.equals("update")){
            update(req,resp);
        }else if(action.equals("delete")){
            delete(req,resp);
        }else if(action.equals("findById")){
            findById(req,resp);
        }else if(action.equals("toAddHouse")){
            toAddHouse(req,resp);
        }else if(action.equals("toAddLess")){
            toAddLess(req,resp);
        }

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        int r_id=Integer.valueOf(req.getParameter("r_id"));
        String sql  ="delete from rend where r_id = ?";
        int i=rendService.delete(sql,r_id);
        System.out.println("i="+i);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void toAddLess(HttpServletRequest req, HttpServletResponse resp) {
        String sql="select l_id,l_name from lessee";
        List<Map> lessee = rendService.toAddLess(sql);
        ResultJson success = ResultJson.success(lessee);
        Utils.returnJson(success,resp);
    }

    private void toAddHouse(HttpServletRequest req, HttpServletResponse resp) {
        String  sql = "select h_id,h_area,h_estate,h_unitNumber from house";
        List<Map> house = rendService.toAddHouse(sql);
        System.out.println(house);
        ResultJson success = ResultJson.success(house);
        Utils.returnJson(success,resp);
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) {
        int r_id=Integer.valueOf(req.getParameter("r_id"));
        String sql = "select r.*,h.h_area,h.h_estate,h.h_unitNumber,l.l_name from rend r,house h,lessee l where r.l_id = l.l_id and r.h_id = h.h_id and r.r_id = ?";
        Map map = rendService.findById(sql,r_id);
        ResultJson success = ResultJson.success(map);
        Utils.returnJson(success,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        int r_id=Integer.valueOf(req.getParameter("r_id"));
        int h_id=Integer.valueOf(req.getParameter("h_id"));
        int l_id=Integer.valueOf(req.getParameter("l_id"));
        String r_money=req.getParameter("r_money");
        String r_time=req.getParameter("r_time");
        String r_remark=req.getParameter("r_remark");
        Object[] obj = {h_id,l_id,r_money,r_time,r_remark,r_id};
        String sql = "update rend set h_id = ?,l_id = ?,r_money = ?,r_time = ?,r_remark = ? where r_id = ?";
        int i=rendService.update(sql,obj);
        System.out.println("i="+i);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        int h_id=Integer.valueOf(req.getParameter("h_id"));
        int l_id=Integer.valueOf(req.getParameter("l_id"));
        String r_money=req.getParameter("r_money");
        String r_time=req.getParameter("r_time");
        String r_remark=req.getParameter("r_remark");
        String sql = "insert into rend values(null,?,?,?,?,?)";
        Object[] obj = {h_id,l_id,r_money,r_time,r_remark};
        int i=rendService.add(sql,obj);
        System.out.println("i="+i);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);

    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = req.getParameter("page") != null ? Integer.valueOf(req.getParameter("page")) : 1;
        Integer limit = req.getParameter("limit") != null ? Integer.valueOf(req.getParameter("limit")) : 10;
        String  l_name =req.getParameter("l_name")!=null?"%"+req.getParameter("l_name")+"%":"%%";
        String sql = "select r.*,h.h_area,h.h_estate,h.h_unitNumber,l.l_name from rend r,house h,lessee l where r.l_id = l.l_id and r.h_id = h.h_id and l.l_name like ?";
        PageBean<Map> pageBean = rendService.findAll(page,limit,sql,l_name);
        System.out.println(pageBean);
        ResultJson success = ResultJson.success(pageBean);
        Utils.returnJson(success,resp);
    }
}
